package com.example.qxfw.common.util;

import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;
import com.example.qxfw.common.entity.DangerSource;
import com.example.qxfw.common.entity.PotentialPoints;
import com.example.qxfw.common.entity.layer.*;
import com.example.qxfw.common.entity.yj.ImpGoal;
import com.example.qxfw.common.entity.zy.FireHistory;
import com.example.qxfw.common.entity.zy.WaterLoggingPointInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.*;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class DocxUtil {

    /**
     * 生成word文档（不含图片插入）
     *
     * @param sourcePath 模板的文件路径
     * @param targetPath 生成的文档要保存的路径
     * @param placeholders 需要替换的文本：这里将需要替换的值存储在了一个数组里
     * @param replacements 替换后的值：这里将需要替换的值存储在了一个数组里
     */
    public static void generateWordDocx(String sourcePath, String targetPath,
                                        String[] placeholders, String[] replacements) {
        try {
            // 读取.docx模板文件
            // Resource resource = new ClassPathResource("templates/nytqTemp/农用天气预报.docx");
            //Resource resource = new ClassPathResource(sourcePath);
            //InputStream inputStream = resource.getInputStream()
            try (InputStream inputStream = new FileInputStream(sourcePath);
                 XWPFDocument doc = new XWPFDocument(inputStream)) {
                for (XWPFParagraph paragraph : doc.getParagraphs()) {
                    replaceTextInParagraph(paragraph, placeholders, replacements);
                }
                for (XWPFTable table : doc.getTables()) {
                    for (XWPFTableRow row : table.getRows()) {
                        for (XWPFTableCell cell : row.getTableCells()) {
                            for (XWPFParagraph paragraph : cell.getParagraphs()) {
                                replaceTextInParagraph(paragraph, placeholders, replacements);
                            }
                        }
                    }
                }
                // 保存文档为.docx文件
                try (OutputStream outputStream = new FileOutputStream(targetPath)) {
                    doc.write(outputStream);
                }
                log.info("Word文档保存成功！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成word文档（含图片插入）
     * @param sourcePath 模板的文件路径
     * @param targetPath 生成的文档要保存的位置
     * @param placeholders 需要替换的文本：这里将需要替换的值存储在了一个数组里
     * @param replacements 替换后的值：这里将需要替换的值存储在了一个数组里
     * @param markerTxt 标记段落，图片就插在这个段落的下方
     * @param imageTxt 图片下方的一段图片说明（可以不说明，根据情况填写）
     * @param image 图片
     * @param width 图片的宽（100对应word中图片的3.53厘米）
     * @param hight 图片的高（100对应word中图片的3.53厘米）
     */
    public static void generateWordDocx(String sourcePath, String targetPath, String[] placeholders,
                 String[] replacements,String markerTxt, String imageTxt, byte[] image, int width, int hight) {
        try {

            // 读取.docx模板文件
            Resource resource = new ClassPathResource(sourcePath);
            try (InputStream inputStream = resource.getInputStream();
                 XWPFDocument doc = new XWPFDocument(inputStream)) {
                for (XWPFParagraph paragraph : doc.getParagraphs()) {
                    replaceTextInParagraph(paragraph, placeholders, replacements);
                }
                for (XWPFTable table : doc.getTables()) {
                    for (XWPFTableRow row : table.getRows()) {
                        for (XWPFTableCell cell : row.getTableCells()) {
                            for (XWPFParagraph paragraph : cell.getParagraphs()) {
                                replaceTextInParagraph(paragraph, placeholders, replacements);
                            }
                        }
                    }
                }

                // 调用查找段落的方法，获得目标段落
                XWPFParagraph targetParagraph = findParagraphByText(doc, markerTxt);
                // 判断段落是否存在
                if (targetParagraph != null){
                    // 判断插入的图片是否为空
                    if (image == null){
                        System.out.println("图片信息为空！！！");
                    }else {
                        // 将 byte 数组转为 InputStream
                        InputStream imageStream = new ByteArrayInputStream(image);
                        // 在标记段落下插入图片和文本
                        insertImageAndText(targetParagraph, imageStream, imageTxt, width, hight);
                    }
                }else {
                    System.out.println("标记段落不存在！！！");
                }
                // 保存文档为.docx文件
                try (OutputStream outputStream = new FileOutputStream(targetPath)) {
                    doc.write(outputStream);
                }
                log.info("Word文档保存成功！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 向word中插图（如果需要多次插图只需要将模板路径和·生成的文档路径设为一个，即可将就的文档覆盖）
     * @param sourcePath 模板的文件路径
     * @param targetPath 生成的文档要保存的位置
     * @param markerTxt 标记段落，图片就插在这个段落的下方
     * @param imageTxt 图片下方的一段图片说明（可以不说明，根据情况填写）
     * @param image 图片
     * @param width 图片的宽（100对应word中图片的3.53厘米）
     * @param hight 图片的高（100对应word中图片的3.53厘米）
     */
    public static void insertImage(String sourcePath, String targetPath,
                  String markerTxt, String imageTxt, byte[] image, int width, int hight) throws FileNotFoundException {
        Resource resource = new ClassPathResource(sourcePath);
        try (InputStream inputStream = resource.getInputStream();
             XWPFDocument doc = new XWPFDocument(inputStream)) {
            // 查找指定文本区域所在的段落
            XWPFParagraph targetParagraph = findParagraphByText(doc, markerTxt);
            if (targetParagraph != null) {
                // 从数据库获取图片的 byte 数据（示例，实际需要替换为你的数据）
                byte[] imageBytes = image;
                if (imageBytes == null) {
                    System.out.println("无数据");
                } else {
                    // 将 byte 数组转为 InputStream
                    InputStream imageStream = new ByteArrayInputStream(imageBytes);
                    // 在指定段落下插入图片和文本
                    insertImageAndText(targetParagraph, imageStream, imageTxt, width, hight);
                    // 将内容[图像和文本]写入Word文档...
                    try (FileOutputStream out = new FileOutputStream(targetPath)) {
                        doc.write(out);
                    }
                }
                System.out.println("Word文档保存成功！");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



        /**
         *docx转换pdf
         */
    public static void wordToPdf(String sourcePath, String targetPath) {
        File inputWord = new File(sourcePath);
        File outputFile = new File(targetPath);
        try {
            InputStream docxInputStream = new FileInputStream(inputWord);
            OutputStream outputStream = new FileOutputStream(outputFile);
            IConverter converter = LocalConverter.builder().build();
            boolean execute = converter.convert(docxInputStream)
                    .as(DocumentType.DOCX)
                    .to(outputStream)
                    .as(DocumentType.PDF).schedule().get();
            outputStream.close();
            docxInputStream.close();

            log.info("转换完毕 targetPath = {}", outputFile.getAbsolutePath());
            System.out.println("转换完毕 targetPath = " + outputFile.getAbsolutePath());
            converter.shutDown();
            return;
        } catch (Exception e) {
            log.error("[documents4J] word转pdf失败:{}", e.toString());
        }
    }

    /**
     * 将文件（文档、图片都可以）转化为文件流------>(适用于较大文件)
     * @param filePath 文件路径
     * @return
     * @throws IOException
     */
    public static InputStream readDocxFileToInputStream(String filePath) throws IOException {
        Path path = Paths.get(filePath);
        byte[] data = Files.readAllBytes(path);
        return new ByteArrayInputStream(data);
    }

    /**
     *  将文件（文档、图片都可以）转化为文件流------>(适用于小文件)
     * @param filePath 文件路径
     * @return
     * @throws IOException
     */
    public static byte[] readDocxFileToByteArray(String filePath) throws Exception {
        try (InputStream inputStream = new FileInputStream(new File(filePath))) {
            // 根据文件长度创建一个合适大小的字节数组
            byte[] buffer = new byte[inputStream.available()];
            // 将文件内容读取到字节数组中
            inputStream.read(buffer);
            return buffer;
        }
    }

    // 替换
    public static void replaceTextInParagraph(XWPFParagraph paragraph, String[] placeholders, String[] replacements) {
        String paragraphText = paragraph.getText();
        for (int i = 0; i < placeholders.length; i++) {
            String regex = Pattern.quote(placeholders[i]);
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(paragraphText);
            if (matcher.find()) {
                paragraphText = matcher.replaceAll(Matcher.quoteReplacement(replacements[i]));
            }
        }
        for (int i = 0; i < paragraph.getRuns().size(); i++) {
            if (i == 0) {
                paragraph.getRuns().get(i).setText(paragraphText, 0);
            } else {
                paragraph.getRuns().get(i).setText("", 0);
            }
        }
    }

    public static void replaceTextInParagraphWithSubscript(XWPFParagraph paragraph, String[] placeholders, String[] replacements){
        for (int i = 0; i < placeholders.length; i++) {
            String placeholder = placeholders[i];
            String replacement = replacements[i];

            for (XWPFRun run : paragraph.getRuns()) {
                String runText = run.getText(0);
                if (runText != null && runText.contains(placeholder)) {
                    String preSubText = runText.substring(0, runText.indexOf(placeholder));
                    String postSubText = runText.substring(runText.indexOf(placeholder) + placeholder.length());
                    run.setText(preSubText + replacement + postSubText, 0);
                }
            }
        }
    }

    // 找到指定要插入的位置即要插入的文本下方
    public static XWPFParagraph findParagraphByText(XWPFDocument doc, String searchText) {
        List<XWPFParagraph> paragraphs = doc.getParagraphs();
        for (int i = 0; i < paragraphs.size(); i++) {
            XWPFParagraph paragraph = paragraphs.get(i);
            if (paragraph.getText().contains(searchText)) {
                if (i + 1 < paragraphs.size()) {
                    return paragraphs.get(i + 1); // 返回下一个段落
                } else {
                    return null; // 如果没有下一个段落，则返回null
                }
            }
        }
        return null;
    }

    // 插入图片和文本
    public static void insertImageAndText(XWPFParagraph paragraph, InputStream imageStream, String textToInsert,int width, int hight ) throws Exception {
        // 在指定段落下创建一个运行
        XWPFRun run = paragraph.createRun();

        // 插入图片(设置图片的宽度和高度第一个为宽，第二个为高 )
        run.addPicture(imageStream, XWPFDocument.PICTURE_TYPE_PNG, "image.png", Units.toEMU(width), Units.toEMU(hight));

        // 设置图片和文本之间的间距
        run.addTab(); // 添加一个制表符，可根据需要调整间距

        // 插入文本
        run.setText(textToInsert);
    }
    // 从类路径加载资源文件（开发和部署时都可以使用，避免部署时的文档读取路径异常的问题）
    //

    /**
     * 设置单元格水平位置和垂直位置
     *
     * @param xwpfTable
     * @param verticalLoction    单元格中内容垂直上TOP，下BOTTOM，居中CENTER，BOTH两端对齐
     * @param horizontalLocation 单元格中内容水平居中center,left居左，right居右，both两端对齐
     */
    public static void setCellLocation(XWPFTable xwpfTable, String verticalLoction, String horizontalLocation) {
        List<XWPFTableRow> rows = xwpfTable.getRows();
        for (XWPFTableRow row : rows) {
            List<XWPFTableCell> cells = row.getTableCells();
            for (XWPFTableCell cell : cells) {
                CTTc cttc = cell.getCTTc();
                CTP ctp = cttc.getPList().get(0);
                CTPPr ctppr = ctp.getPPr();
                if (ctppr == null) {
                    ctppr = ctp.addNewPPr();
                }
                CTJc ctjc = ctppr.getJc();
                if (ctjc == null) {
                    ctjc = ctppr.addNewJc();
                }
                ctjc.setVal(STJc.Enum.forString(horizontalLocation)); //水平居中
                cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.valueOf(verticalLoction));//垂直居中
            }
        }
    }

    /**
     * 设置表格位置
     *
     * @param xwpfTable
     * @param location  整个表格居中center,left居左，right居右，both两端对齐
     */
    public static void setTableLocation(XWPFTable xwpfTable, String location) {
        CTTbl cttbl = xwpfTable.getCTTbl();
        CTTblPr tblpr = cttbl.getTblPr() == null ? cttbl.addNewTblPr() : cttbl.getTblPr();
        CTJc cTJc = tblpr.addNewJc();
        cTJc.setVal(STJc.Enum.forString(location));
    }

    /**
     * 插入表格图层数据
     * @param layerName
     * @param layerObjectList
     * @param table
     */
    public static void insertTablaLayerData(String layerName,List<Object> layerObjectList,XWPFTable table){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //添加表头
        XWPFTableRow row_0 = table.getRow(0);
        Class objectClass = null;
        switch (layerName) {
            case "村庄":
                row_0.getCell(0).setText("唯一编号");
                row_0.addNewTableCell().setText("名称");
                row_0.addNewTableCell().setText("村长");
                row_0.addNewTableCell().setText("联系电话");
                objectClass = VillagePointLayer.class;
                Class finalObjectClass = objectClass;
                layerObjectList.forEach(layerObject -> {
                    try {
                        Field dmgisId = finalObjectClass.getDeclaredField("dmgis_id");
                        Field name = finalObjectClass.getDeclaredField("NAME");
                        Field chief = finalObjectClass.getDeclaredField("CHIEF");
                        Field tel = finalObjectClass.getDeclaredField("TELPHONE");
                        dmgisId.setAccessible(true);
                        name.setAccessible(true);
                        chief.setAccessible(true);
                        tel.setAccessible(true);
                        XWPFTableRow dataRow = table.createRow();
                        dataRow.getCell(0).setText(String.valueOf(dmgisId.get(layerObject)));
                        dataRow.getCell(1).setText(String.valueOf(name.get(layerObject)));
                        dataRow.getCell(2).setText(String.valueOf(chief.get(layerObject)));
                        dataRow.getCell(3).setText(String.valueOf(tel.get(layerObject)));
                    } catch (Exception e) {
                        System.err.println("插入地理分析表格错误");
                    }
                });
                break;
            case "乡界面":
                row_0.getCell(0).setText("唯一编号");
                row_0.addNewTableCell().setText("名称");
                row_0.addNewTableCell().setText("");
                row_0.addNewTableCell().setText("");
                objectClass = TownshipLayer.class;
                Class finalObjectClass1 = objectClass;
                layerObjectList.forEach(layerObject -> {
                    try {
                        Field dmgisId = finalObjectClass1.getDeclaredField("dmgis_id");
                        Field name = finalObjectClass1.getDeclaredField("NAME");
                        dmgisId.setAccessible(true);
                        name.setAccessible(true);
                        XWPFTableRow dataRow = table.createRow();
                        dataRow.getCell(0).setText(String.valueOf(dmgisId.get(layerObject)));
                        dataRow.getCell(1).setText(String.valueOf(name.get(layerObject)));
                    } catch (Exception e) {
                        System.err.println("插入地理分析表格错误");
                    }
                });
                break;
            case "铁路":
                row_0.getCell(0).setText("唯一编号");
                row_0.addNewTableCell().setText("名称");
                row_0.addNewTableCell().setText("");
                row_0.addNewTableCell().setText("");
                objectClass = RailwayLayer.class;
                Class finalObjectClass2 = objectClass;
                layerObjectList.forEach(layerObject -> {
                    try {
                        Field dmgisId = finalObjectClass2.getDeclaredField("dmgis_id");
                        Field name = finalObjectClass2.getDeclaredField("NAME");
                        dmgisId.setAccessible(true);
                        name.setAccessible(true);
                        XWPFTableRow dataRow = table.createRow();
                        dataRow.getCell(0).setText(String.valueOf(dmgisId.get(layerObject)));
                        dataRow.getCell(1).setText(String.valueOf(name.get(layerObject)));
                    } catch (Exception e) {
                        System.err.println("插入地理分析表格错误");
                    }
                });
                break;
            case "乡村道路":
                row_0.getCell(0).setText("唯一编号");
                row_0.addNewTableCell().setText("名称");
                row_0.addNewTableCell().setText("");
                row_0.addNewTableCell().setText("");
                objectClass = CountryRoadLayer.class;
                Class finalObjectClass3 = objectClass;
                layerObjectList.forEach(layerObject -> {
                    try {
                        Field dmgisId = finalObjectClass3.getDeclaredField("dmgis_id");
                        Field name = finalObjectClass3.getDeclaredField("NAME");
                        dmgisId.setAccessible(true);
                        name.setAccessible(true);
                        XWPFTableRow dataRow = table.createRow();
                        dataRow.getCell(0).setText(String.valueOf(dmgisId.get(layerObject)));
                        dataRow.getCell(1).setText(String.valueOf(name.get(layerObject)));
                    } catch (Exception e) {
                        System.err.println("插入地理分析表格错误");
                    }
                });
                break;
            case "国道":
                row_0.getCell(0).setText("唯一编号");
                row_0.addNewTableCell().setText("名称");
                row_0.addNewTableCell().setText("");
                row_0.addNewTableCell().setText("");
                objectClass = NationalRoadLayer.class;
                Class finalObjectClass4 = objectClass;
                layerObjectList.forEach(layerObject -> {
                    try {
                        Field dmgisId = finalObjectClass4.getDeclaredField("dmgis_id");
                        Field name = finalObjectClass4.getDeclaredField("NAME");
                        dmgisId.setAccessible(true);
                        name.setAccessible(true);
                        XWPFTableRow dataRow = table.createRow();
                        dataRow.getCell(0).setText(String.valueOf(dmgisId.get(layerObject)));
                        dataRow.getCell(1).setText(String.valueOf(name.get(layerObject)));
                    } catch (Exception e) {
                        System.err.println("插入地理分析表格错误");
                    }
                });
                break;
            case "省道":
                row_0.getCell(0).setText("唯一编号");
                row_0.addNewTableCell().setText("名称");
                row_0.addNewTableCell().setText("");
                row_0.addNewTableCell().setText("");
                objectClass = ProvincialRoadLayer.class;
                Class finalObjectClass5 = objectClass;
                layerObjectList.forEach(layerObject -> {
                    try {
                        Field dmgisId = finalObjectClass5.getDeclaredField("dmgis_id");
                        Field name = finalObjectClass5.getDeclaredField("NAME");
                        dmgisId.setAccessible(true);
                        name.setAccessible(true);
                        XWPFTableRow dataRow = table.createRow();
                        dataRow.getCell(0).setText(String.valueOf(dmgisId.get(layerObject)));
                        dataRow.getCell(1).setText(String.valueOf(name.get(layerObject)));
                    } catch (Exception e) {
                        System.err.println("插入地理分析表格错误");
                    }
                });
                break;
            case "高速":
                row_0.getCell(0).setText("唯一编号");
                row_0.addNewTableCell().setText("名称");
                row_0.addNewTableCell().setText("");
                row_0.addNewTableCell().setText("");
                objectClass = HighwayLayer.class;
                Class finalObjectClass6 = objectClass;
                layerObjectList.forEach(layerObject -> {
                    try {
                        Field dmgisId = finalObjectClass6.getDeclaredField("dmgis_id");
                        Field name = finalObjectClass6.getDeclaredField("NAME");
                        dmgisId.setAccessible(true);
                        name.setAccessible(true);
                        XWPFTableRow dataRow = table.createRow();
                        dataRow.getCell(0).setText(String.valueOf(dmgisId.get(layerObject)));
                        dataRow.getCell(1).setText(String.valueOf(name.get(layerObject)));
                    } catch (Exception e) {
                        System.err.println("插入地理分析表格错误");
                    }
                });
                break;
            case "县道":
                row_0.getCell(0).setText("唯一编号");
                row_0.addNewTableCell().setText("名称");
                row_0.addNewTableCell().setText("");
                row_0.addNewTableCell().setText("");
                objectClass = CountyRoadLayer.class;
                Class finalObjectClass7 = objectClass;
                layerObjectList.forEach(layerObject -> {
                    try {
                        Field dmgisId = finalObjectClass7.getDeclaredField("dmgis_id");
                        Field name = finalObjectClass7.getDeclaredField("NAME");
                        dmgisId.setAccessible(true);
                        name.setAccessible(true);
                        XWPFTableRow dataRow = table.createRow();
                        dataRow.getCell(0).setText(String.valueOf(dmgisId.get(layerObject)));
                        dataRow.getCell(1).setText(String.valueOf(name.get(layerObject)));
                    } catch (Exception e) {
                        System.err.println("插入地理分析表格错误");
                    }
                });
                break;
            case "水系":
                row_0.getCell(0).setText("唯一编号");
                row_0.addNewTableCell().setText("名称");
                row_0.addNewTableCell().setText("");
                row_0.addNewTableCell().setText("");
                objectClass = WaterSystemLayer.class;
                Class finalObjectClass8 = objectClass;
                layerObjectList.forEach(layerObject -> {
                    try {
                        Field dmgisId = finalObjectClass8.getDeclaredField("dmgis_id");
                        Field name = finalObjectClass8.getDeclaredField("NAME");
                        dmgisId.setAccessible(true);
                        name.setAccessible(true);
                        XWPFTableRow dataRow = table.createRow();
                        dataRow.getCell(0).setText(String.valueOf(dmgisId.get(layerObject)));
                        dataRow.getCell(1).setText(String.valueOf(name.get(layerObject)));
                    } catch (Exception e) {
                        System.err.println("插入地理分析表格错误");
                    }
                });
                break;
            case "重要目标":
                row_0.getCell(0).setText("唯一编号");
                row_0.addNewTableCell().setText("名称");
                row_0.addNewTableCell().setText("负责人");
                row_0.addNewTableCell().setText("联系电话");
                layerObjectList.forEach(layerObject -> {
                    try {
                        ImpGoal impGoal = (ImpGoal) layerObject;
                        XWPFTableRow dataRow = table.createRow();
                        dataRow.getCell(0).setText(String.valueOf(impGoal.getId()));
                        dataRow.getCell(1).setText(impGoal.getTargettype());
                        dataRow.getCell(2).setText(impGoal.getPrincipalname());
                        dataRow.getCell(3).setText(impGoal.getTel());
                    } catch (Exception e) {
                        System.err.println("插入地理分析表格错误");
                    }
                });
                break;
            case "灾害点":
                row_0.getCell(0).setText("唯一编号");
                row_0.addNewTableCell().setText("灾害名称");
                row_0.addNewTableCell().setText("所属县");
                row_0.addNewTableCell().setText("监测人");
                row_0.addNewTableCell().setText("监测人电话");
                row_0.addNewTableCell().setText("群测群防人");
                row_0.addNewTableCell().setText("群测群防人电话");
                layerObjectList.forEach(layerObject -> {
                    try {
                        PotentialPoints potentialPoints = (PotentialPoints) layerObject;
                        XWPFTableRow dataRow = table.createRow();
                        dataRow.getCell(0).setText(potentialPoints.getPpId());
                        dataRow.getCell(1).setText(potentialPoints.getPpName());
                        dataRow.getCell(2).setText(potentialPoints.getCounty());
                        dataRow.getCell(3).setText(potentialPoints.getContact());
                        dataRow.getCell(4).setText(potentialPoints.getContactTel());
                        dataRow.getCell(5).setText(potentialPoints.getContact1());
                        dataRow.getCell(6).setText(potentialPoints.getContact1Tel());
                    } catch (Exception e) {
                        System.err.println("插入地理分析表格错误");
                    }
                });
                break;
            case "危险源":
                row_0.getCell(0).setText("唯一编号");
                row_0.addNewTableCell().setText("危险源名称");
                row_0.addNewTableCell().setText("监测人");
                row_0.addNewTableCell().setText("监测人联系方式");
                layerObjectList.forEach(layerObject -> {
                    try {
                        DangerSource dangerSource = (DangerSource) layerObject;
                        XWPFTableRow dataRow = table.createRow();
                        dataRow.getCell(0).setText(String.valueOf(dangerSource.getId()));
                        dataRow.getCell(1).setText(dangerSource.getSourcename());
                        dataRow.getCell(2).setText(dangerSource.getPrincipal());
                        dataRow.getCell(3).setText(dangerSource.getTel());
                    } catch (Exception e) {
                        System.err.println("插入地理分析表格错误");
                    }
                });
                break;
            case "易涝点":
                row_0.getCell(0).setText("易涝点名称");
                row_0.addNewTableCell().setText("所属县");
                row_0.addNewTableCell().setText("监测人");
                row_0.addNewTableCell().setText("监测人联系方式");
                layerObjectList.forEach(layerObject -> {
                    try {
                        WaterLoggingPointInfo waterLoggingPointInfo = (WaterLoggingPointInfo) layerObject;
                        XWPFTableRow dataRow = table.createRow();
                        dataRow.getCell(0).setText(String.valueOf(waterLoggingPointInfo.getId()));
                        dataRow.getCell(1).setText(waterLoggingPointInfo.getName());
                        dataRow.getCell(2).setText(waterLoggingPointInfo.getPrincipal());
                        dataRow.getCell(3).setText(waterLoggingPointInfo.getTel());
                    } catch (Exception e) {
                        System.err.println("插入地理分析表格错误");
                    }
                });
                break;
            case "历史火点":
                row_0.getCell(0).setText("发生时间");
                row_0.addNewTableCell().setText("历史火点名称");
                row_0.addNewTableCell().setText("受灾面积");
                row_0.addNewTableCell().setText("威胁人口");
                layerObjectList.forEach(layerObject -> {
                    try {
                        FireHistory fireHistory = (FireHistory) layerObject;
                        XWPFTableRow dataRow = table.createRow();
                        dataRow.getCell(0).setText(sdf.format(fireHistory.getAnnouncedtime()));
                        dataRow.getCell(1).setText(fireHistory.getFirename());
                        dataRow.getCell(2).setText(String.valueOf(fireHistory.getDisasterarea()));
                        dataRow.getCell(3).setText(String.valueOf(fireHistory.getPopthreat()));
                    } catch (Exception e) {
                        System.err.println("插入地理分析表格错误");
                    }
                });
                break;
        }
    }

}

